//
//  GRReturnViewController.m
//  YC_GRice
//
//  Created by luojinrong11 on 2018/7/24.
//  Copyright © 2018年 YeCai. All rights reserved.
//

#import "GRReturnViewController.h"
#import "GRReturnTimeCell.h"
#import "RequestManager+GROrderManager.h"
#import "GROrderReturnModel.h"
#import "GRReturnResonModel.h"
@interface GRReturnViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    NSString * payWayStr;
    NSString * returnWayStr;
}
@property (weak, nonatomic) IBOutlet UITableView *ReturnProgressList;

@property (strong, nonatomic) IBOutlet UIView *headerView;

@property(nonatomic,strong)GROrderReturnModel * orderReturnModel;

@property (weak, nonatomic) IBOutlet UIView *returnMsgView;
@property(nonatomic,strong)NSArray * titleArr;

@property (weak, nonatomic) IBOutlet UIButton *ReturnBtF;
@property (weak, nonatomic) IBOutlet UIButton *ReturnBtS;
@property (weak, nonatomic) IBOutlet UILabel *resonLabel;
@property (strong, nonatomic) IBOutlet UIView *footView;

@property (weak, nonatomic) IBOutlet UILabel *returnLable;

@end

@implementation GRReturnViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    [self.ReturnBtF.layer setBorderWidth:1];
    [self.ReturnBtF.layer setBorderColor:HEXCOLOR(0xDDDDDD).CGColor];
    [self.ReturnBtS.layer setBorderWidth:1];
    [self.ReturnBtS.layer setBorderColor:HEXCOLOR(0xDDDDDD).CGColor ];
    // Do any additional setup after loading the view from its nib.
    if (_isReturnGood) {
        self.headerView.frame = CGRectMake(0, 0, KscreenWidth, 287);
        self.navigationItem.title = @"退货进度";
        self.returnLable.text = @"退货进度";
       
    }else{
        self.headerView.frame = CGRectMake(0, 0, KscreenWidth, 236);
        self.navigationItem.title = @"退款进度";
        self.returnLable.text = @"退款进度";
    }
    self.ReturnProgressList.tableHeaderView = self.headerView;
    self.ReturnProgressList.tableFooterView = [[UIView alloc]init];
    [self.ReturnProgressList registerNib:[UINib nibWithNibName:@"GRReturnTimeCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"timeCell"];
    [self loadData];
}

-(void)loadData{
    [RequestManager orderReturnProgress:_isReturnGood ?@"orderInfo":@"order"  OrderId:_orderId CompleteHander:^(BOOL success, id responseObject, GRError *error) {
        if (success) {
            NSLog(@"%@", responseObject);
            self.orderReturnModel = [GROrderReturnModel yy_modelWithDictionary:responseObject];
            if (self.orderReturnModel.payType ==1) {
                payWayStr = @"微信支付";
            }else if (self.orderReturnModel.payType ==2){
                payWayStr = @"等待退货";
            }else if (self.orderReturnModel.payType == 3){
                payWayStr = @"纯积分支付";
            }else{
                payWayStr = @"微信支付小程序";
            }
            if (self.orderReturnModel.returnType == 1) {
                returnWayStr = @"原路退回";
            }
                self.titleArr = @[[NSString stringWithFormat:@"￥%.2lf",self.orderReturnModel.firstGive],[NSString stringWithFormat:@"￥%.2lf",self.orderReturnModel.point],@"24小时内",payWayStr,returnWayStr,self.orderReturnModel.paySn];
            for (int i = 0; i <self.titleArr.count; i++) {
                UILabel * label = [self.returnMsgView viewWithTag:i+1000];
                label.text = self.titleArr[i];
            }
            [self.ReturnProgressList reloadData];
            if (self.orderReturnModel.status == 3 ||self.orderReturnModel.status ==4) {
                self.headerView.frame = CGRectMake(0, 0, KscreenWidth, 236);
                self.ReturnProgressList.tableHeaderView = self.headerView;
                if (self.orderReturnModel.status == 4) {
                    self.ReturnProgressList.tableFooterView = self.footView;
                    self.resonLabel.text = [NSString stringWithFormat:@"原因:%@",self.orderReturnModel.closeMemo];
                }
            }else{
                if (self.orderReturnModel.status == 2) {
                    self.ReturnBtF.hidden = YES;
                    [self.ReturnBtS setTitle:@"联系客服" forState: UIControlStateNormal];
                }else{
                  [self.ReturnBtS setTitle:@"取消退货" forState: UIControlStateNormal];
                  [self.ReturnBtF setTitle:@"联系客服" forState: UIControlStateNormal];
                }
            }
           
        }
    }];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    GRReturnTimeCell * timeCell = [tableView dequeueReusableCellWithIdentifier:@"timeCell" forIndexPath:indexPath];
    if (indexPath.row == 0) {
        timeCell.topLine.hidden = YES;
        
    }else{
        timeCell.topLine.hidden = NO;
    }
    if (indexPath.row == self.orderReturnModel.handDetails.count -1 ) {
            timeCell.bottomLine.hidden = YES;
        timeCell.colorView.hidden = NO;
        timeCell.textLabel.textColor = HEXCOLOR(0x222222);
    
    }else{
        timeCell.bottomLine.hidden = NO;
        timeCell.colorView.hidden = YES;
        timeCell.textLabel.textColor = HEXCOLOR(0x999999);
    }
    GRLogsticProgresModel * model = self.orderReturnModel.handDetails[indexPath.row];
    timeCell.titleLabel.text = model.msg;
    timeCell.rightTimeLabel.text = model.timer;
    return timeCell;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.orderReturnModel.handDetails.count;

}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}

- (IBAction)ReturnAction:(UIButton *)sender {
    if ([sender.titleLabel.text isEqualToString:@"取消退货"]) {
        [[LFAlertBox defaultAlert] showAlertContent:@"是否取消退货" cancle:@"否" sure:@"是" completion:^(NSInteger index) {
            if (index) {
                [RequestManager cancleReturn:self.orderReturnModel.id CompleteHander:^(BOOL success, id responseObject, GRError *error) {
                    if (success) {
                        NSLog(@"取消退货成功");
                        [self loadData];
                        if (self.returnSuccess !=nil) {
                            self.returnSuccess();
                        }
                    }
                }];
            }
        }];
      
    }else{
           [NSObject openSystemURL:@"telprompt://4006609727"];
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
